package com.forte.deliverydao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;

@Transactional
public class DeliveryDAOImpl implements DeliveryDAO{
	
	private SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory sessionFactory){
		this.sessionFactory = sessionFactory;
	}
	
	protected final Session getCurrentSession(){
		return sessionFactory.getCurrentSession();
	}
	
	public void saveOrUpdate(Delivery delivery){
		getCurrentSession().saveOrUpdate(delivery);
	}
	
	public Delivery findDeliveryById(int deliveryId){
		Query query = getCurrentSession().createQuery(" from delivery where deliveryId = :deliveryId");
		query.setParameter("deliveryId", deliveryId);
		
		return foundDelivery(query.list());		
	}
	
	public Delivery foundDelivery(List list){
		Delivery delivFound = null;
		if ( list.size() > 0){
			delivFound = (Delivery) list.get(0);
		}
		return delivFound;
	}
	
	

}
